--[[
	Author: Elsia
	BattleObjectivesMover, moves the Battle Objectives Frame.
--]]

BattleObjectivesMover = LibStub("AceAddon-3.0"):NewAddon("BattleObjectivesMover", "AceConsole-3.0")
local AceConfigDialog = LibStub("AceConfigDialog-3.0")

local Default = {
	profile =
	{
		locked = true,
		pos	= { x = WorldStateAlwaysUpFrame:GetLeft(), y = WorldStateAlwaysUpFrame:GetTop()},
	}
}

if not deepcopy then
function deepcopy(object)
	local lookup_table = {}
	local function _copy(object)
		if type(object) ~= "table" then
			return object
		elseif lookup_table[object] then
			return lookup_table[object]
		end
		local new_table = {}
		lookup_table[object] = new_table
		for index, value in pairs(object) do
			new_table[_copy(index)] = _copy(value)
		end
		return setmetatable(new_table, getmetatable(object))
	end
	return _copy(object)
end
end

function BattleObjectivesMover:OnInitialize()

	self.f = WorldStateAlwaysUpFrame

	local Options = {
		name = "BattleObjectivesMover",
		type = "group",
		args = {
	        lock = {
	            name = "Lock",
	            type = "toggle",
	            desc = "Lock/Unlock the battle objectives frame.",
	            get = function() return self.db.profile.locked end,
	            set = function( _,v ) self.db.profile.locked = v if(v) then
		if self.master  then self.master:EnableMouse(false)
		self:HideDragger() self:SetPosition() end else self:ShowDragger()
		self.master:EnableMouse(true) end end,
            },
		}
	}
	
	local Options2 = deepcopy(Options)
	
	local acedb = LibStub:GetLibrary("AceDB-3.0")
	self.db = acedb:New("BattleObjectivesMoverDB", Default)
	self.db.RegisterCallback( self, "OnNewProfile", "HandleProfileChanges" )
	self.db.RegisterCallback( self, "OnProfileReset", "HandleProfileChanges" )
	self.db.RegisterCallback( self, "OnProfileChanged", "HandleProfileChanges" )
	self.db.RegisterCallback( self, "OnProfileCopied", "HandleProfileChanges" )

	Options2.args.profile = LibStub("AceDBOptions-3.0"):GetOptionsTable(self.db)
	LibStub("AceConfig-3.0"):RegisterOptionsTable("BattleObjectivesMover", Options2, "bom")
 	LibStub("AceConfigRegistry-3.0"):RegisterOptionsTable("BattleObjectivesMover Blizz", Options)

	LibStub("AceConfigRegistry-3.0"):RegisterOptionsTable("BattleObjectivesMover Profile", Options2.args.profile)
	AceConfigDialog:AddToBlizOptions("BattleObjectivesMover Blizz", "BattleObjectivesMover")
	AceConfigDialog:AddToBlizOptions("BattleObjectivesMover Profile", "Profile", "BattleObjectivesMover")
	
end

function BattleObjectivesMover:HandleProfileChanges()
		if(self.db.profile.locked) then
			if self.master then
				self.master:EnableMouse(false)
				self:HideDragger()
				self:SetPosition()
			end
		else 
			self:ShowDragger()
			self.master:EnableMouse(true)
		end
end

function BattleObjectivesMover:ShowDragger()

	if not self.master then    -- Only creates frame when dragging is enabled.
	   self:CreateFrame()
	end

	self:ReparentFrame()
    self:SetPosition()

	self.master:SetBackdropBorderColor(TOOLTIP_DEFAULT_COLOR.r, TOOLTIP_DEFAULT_COLOR.g, TOOLTIP_DEFAULT_COLOR.b, 0.5)
	self.master:SetBackdropColor(TOOLTIP_DEFAULT_BACKGROUND_COLOR.r, TOOLTIP_DEFAULT_BACKGROUND_COLOR.g, TOOLTIP_DEFAULT_BACKGROUND_COLOR.b, 0.5)
	self.master.label:Show()
end

function BattleObjectivesMover:HideDragger()
	self.master:SetBackdropBorderColor(0,0,0,0)
	self.master:SetBackdropColor(0,0,0,0)
	self.master.label:Hide()
end

function BattleObjectivesMover:ReparentFrame()
	self.f:ClearAllPoints()
	self.f:SetParent(self.master)
	local s = self.f:GetEffectiveScale()
	self.f:ClearAllPoints()
	self.f:SetPoint("TOPLEFT", self.master, "TOPLEFT", 0, 0)
	
end

function BattleObjectivesMover:CreateFrame()
	self.master = CreateFrame("Frame", "BattleObjectivesMoverFrame", UIParent)

	self.master:SetMovable(true)

	self.master:RegisterForDrag("LeftButton")
	self.master:SetScript("OnDragStart", function() if not self.db.profile.locked then self["master"]:StartMoving() end end)
	self.master:SetScript("OnDragStop", function() self["master"]:StopMovingOrSizing() self:SavePosition() end)
		
	self.master.label = self.master:CreateFontString(nil, "ARTWORK")
	self.master.label:SetFontObject(TextStatusBarText)
    
	self.master:SetWidth( 150 )
	self.master:SetHeight( 75 )

	self.master:SetBackdrop({
			bgFile = "Interface\\Tooltips\\UI-Tooltip-Background", tile = true, tileSize = 8,
			edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", 
			edgeSize = 16,
			insets = {left = 4, right = 4, top = 4, bottom = 4},
		})
	self.master:SetBackdropBorderColor(TOOLTIP_DEFAULT_COLOR.r, TOOLTIP_DEFAULT_COLOR.g, TOOLTIP_DEFAULT_COLOR.b, 0.5)
	self.master:SetBackdropColor(TOOLTIP_DEFAULT_BACKGROUND_COLOR.r, TOOLTIP_DEFAULT_BACKGROUND_COLOR.g, TOOLTIP_DEFAULT_BACKGROUND_COLOR.b, 0.5)

	self.master.label:SetJustifyH("CENTER")
	local labelFont, osize, oflags = GameFontHighlightSmall:GetFont()
	self.master.label:SetFont( labelFont, osize, oflags )
	self.master.label:ClearAllPoints()
	self.master.label:SetPoint("CENTER", self.master, "CENTER",0,0)
	self.master.label:SetTextColor( 1,1,1 )
	self.master.label:SetText( "Moveable Battle Objectives" )
	
	self.master:Show()
end

function BattleObjectivesMover:OnEnable()

	 if not self.db.profile.locked then
	    self:ShowDragger()
	    self.master:EnableMouse(true)
	 end
	 self:SetPosition()
end

function BattleObjectivesMover:SavePosition()

	local x, y = self.master:GetLeft(), self.master:GetTop()
	local s = self.master:GetEffectiveScale()
	
	self.db.profile.pos.x = x * s
	self.db.profile.pos.y = y * s

	self:SetPosition() -- Not sure if this is needed
end

function BattleObjectivesMover:SetPosition()

	if self.db.profile.pos.x then
		local x = self.db.profile.pos.x
		local y = self.db.profile.pos.y

		local s = self.f:GetEffectiveScale()

		if self.db.profile.locked then
			self.f:ClearAllPoints()
			self.f:SetParent(UIParent)
			self.f:ClearAllPoints()
			self.f:SetPoint("TOPLEFT", UIParent, "BOTTOMLEFT", x / s, y / s) -- Seems like the frame is forcing "TOP" :P
		else
			self.master:ClearAllPoints()
			self.master:SetPoint("TOPLEFT", UIParent, "BOTTOMLEFT", x / s, y / s)
		end
	end
end
